package com.yst.shader.feature.white_board

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import com.facebook.drawee.generic.GenericDraweeHierarchy
import com.yst.shader.R
import com.yst.shader.ext.randomChar
import com.yst.shader.feature.ss.effect.SSImageView
import java.util.Collections.min
import kotlin.random.Random

/**
 * @author: xuyujian@ysten.com
 *  @date: 2020/8/28 15:24
 */
class WhiteBoardImageView : SSImageView {


    private var title = ""
    private var titleColor = Color.BLACK
    private var titleSize = 32
    private var subTitle = ""
    private var subTitleColor = Color.GRAY
    private var subTitleSize = 24

    private var boardColor = Color.WHITE
    private var boardTitleColor = Color.BLACK
    private var boardSubTitleColor = Color.DKGRAY

    var textPadding = 12f
    var boardPadding = 12f

    var isInnerBoard = true //外标题

    val MaxTiltLines = 2


    private val mPaint = Paint().apply {
        isDither = true
        isAntiAlias = true
    }

    constructor(context: Context?, hierarchy: GenericDraweeHierarchy?) : super(context, hierarchy)
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        val obtainStyledAttributes =
            context?.obtainStyledAttributes(attrs, R.styleable.WhiteBoardImageView) ?: return


        obtainStyledAttributes.let {
            title = it.getString(R.styleable.WhiteBoardImageView_title).orEmpty()
            titleColor = it.getColor(R.styleable.WhiteBoardImageView_titleTextColor, titleColor)
            titleSize =
                it.getDimensionPixelSize(R.styleable.WhiteBoardImageView_titleSize, titleSize)
            subTitle = it.getString(R.styleable.WhiteBoardImageView_subtitle).orEmpty()
            subTitleColor =
                it.getColor(R.styleable.WhiteBoardImageView_subTitleTextColor, subTitleColor)
            subTitleSize =
                it.getDimensionPixelSize(R.styleable.WhiteBoardImageView_subTitleSize, subTitleSize)
            boardColor = it.getColor(R.styleable.WhiteBoardImageView_boardColor, boardColor)
            boardTitleColor =
                it.getColor(R.styleable.WhiteBoardImageView_boardTitleColor, boardTitleColor)
            boardSubTitleColor =
                it.getColor(R.styleable.WhiteBoardImageView_boardSubTitleColor, boardSubTitleColor)
        }


        obtainStyledAttributes.recycle()

        if (title.isNullOrBlank()) {
            title = anyWords()
        }


        if (subTitle.isNullOrBlank()) {
            subTitle = anyWords()
        }
    }


    private val r = Random(System.currentTimeMillis())


    private fun anyWords() = randomChar(r.nextInt(5, 30))

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //draw
        println("measure title:$title , isouter :$isInnerBoard")
        val w = width
        val h = height
        mPaint.textSize = titleSize.toFloat()
        var txtW = mPaint.measureText(title, 0, title.length)
        println("measure w: $w , h:$h , ,size :${mPaint.textSize} txtW:$txtW")

        mPaint.textSize = titleSize.toFloat()
        txtW = mPaint.measureText(title, 0, title.length)
        println("measure w: $w , h:$h , ,size :${mPaint.textSize} txtW:$txtW")

        if (mShadowFocused) {
            //白板
            mPaint.color = boardColor
            mPaint.style = Paint.Style.FILL
            val txtH = titleSize
            val txtW = titleSize
            val txtDrawLength = ((w - 2 * textPadding) / txtW).toInt().coerceAtMost(title.length)
            val twoLines = txtDrawLength < title.length

            val boardH = if (twoLines){ txtH *2 + boardPadding * 2} else {txtH + boardPadding * 2}
            val drawRectY = if (isInnerBoard) {
                h - boardH
            } else {
                h.toFloat()
            }
            canvas.drawRect(0f, drawRectY, w.toFloat(), drawRectY + boardH, mPaint)

            mPaint.color = boardTitleColor
            val fm = mPaint.fontMetrics
            val textDrawY = if (isInnerBoard) {
                h - boardPadding - fm.descent / 2
            } else {
                h + boardPadding + txtH - fm.descent / 2
            }

            //first line
            canvas.drawText(
                title, 0, txtDrawLength,
                textPadding, textDrawY, mPaint
            )
            val nTxt = if (txtDrawLength * 2 < title.length) {
                title.substring(0, txtDrawLength * 2 - 1) + "..."
            } else {
                title
            }
            if (twoLines) {
                //line 2
                canvas.drawText(
                    nTxt, txtDrawLength, txtDrawLength,
                    textPadding, textDrawY + txtH, mPaint
                )
            }


        } else {
            mPaint.color = titleColor
            val fm = mPaint.fontMetrics
            val txtDrawY = if (isInnerBoard) {
                h - boardPadding - fm.descent / 2
            } else {
                h +  textPadding - fm.descent / 2
            }
            canvas.drawText(
                title, 0, title.length,
                textPadding.toFloat(), txtDrawY, mPaint
            )
        }

    }


    private fun Canvas.showTextLines() {
        //            mPaint.color = Color.RED
//            canvas.drawLine(0f, textDrawY ,30000f,textDrawY ,mPaint)
//            mPaint.color = Color.GREEN
//            canvas.drawLine(0f, textDrawY + fm.ascent ,30000f,textDrawY ,mPaint)
//            mPaint.color = Color.YELLOW
//            canvas.drawLine(0f, textDrawY + fm.descent ,30000f,textDrawY ,mPaint)
    }
}